<?php

$this->AddHook('other_work', 'Sell');

function SellObject($bot, $obj) 
    {
      $settings = (array) $bot->ld->GetPlSettings("Sellplugin");
      
      if ($settings["SellTo"] == "0") 
      {$action = "sell"; $flag = "[Sell] Object(s) to Sell"; }
      
      if ($settings["SellTo"] == "1")
      {$action = "sendToInventory"; $flag = "[Sell] Object(s) to Inventory"; }
      
      $fname = '';
      $bot->SendMsg('[Sell] Selling Item Id: '.$obj['id'].' - '.$obj['itemName'].$fname);
      $amf = new AMFObject("");
      $amf->_bodys[0] = new MessageBody();
      $amf->_bodys[0]->_value[0] = $bot->GetAmfHeader();
      $amf->_bodys[0]->targetURI = 'BaseService.dispatchBatch';
      $amf->_bodys[0]->responseURI = '/1';
      $amf->_bodys[0]->_value[2] = 0;
      $amf->_bodys[0]->_value[1][0]['sequence'] = $bot->GetSequense();
      $amf->_bodys[0]->_value[1][0]['functionName'] = "WorldService.performAction";
      $bot->SendMsg ($flag);
      $amf->_bodys[0]->_value[1][0]['params'][0] = $action;
      $amf->_bodys[0]->_value[1][0]['params'][1] = $obj;
      $amf->_bodys[0]->_value[1][0]['params'][2] = Array();

      $serializer = new AMFSerializer();
      $result = $serializer->serialize($amf);

      $x = $bot->SendRequest($result);
      $x = $bot->Deserialize($x);
      $bot->CheckServerError($x);
      $bot->Add2Report('Sell Object');

      return $x;
    } 
    
function Sell($bot) 
    {
	    if ($bot->firstrun)
	    { 
		  $Name = "Sellplugin"; $Version = "1.2"; $Date = "01.05.2012";
		  $bot->ld->UpdatePluginVersion($bot, $Name, $Version, $Date )  ;
		  return;
	    }

      $msg = $bot->irc('u') . $bot->irc('b');
	    $msg .= $bot->irc('cc') . '2';
	    $msg .= $bot->irc('ff') . '13,5' . chr(232) . $bot->irc('ff') . '14,5';
	    $msg .= 'Starting Plugin: Sell';
	    $bot->SendMsg($msg);
      
      $data = $bot->ld->GetPlSettings("Sellplugin");
      $stop = "Y";
      if (!isset($data->Sell))  { $data->pause = 1; 
       
          $bot->ld->SavePlSettings("Sellplugin", $data); return;}
          
      if ($data->Sell == 0  ) { 
      
          $bot->SendMsg('Sell: no Action to be done'); return;}
      
      if (!isset($data->itemList)) {$itemList = 0; }
      if (empty($data->itemList)) {$itemList = 0; }
      
          $itemList    = $data->itemList;
      
      if (!isset($data->dt_itemList)) {$dt_itemList = 0; }
      if (empty($data->dt_itemList)) {$dt_itemList = 0; }
      
         $dt_itemList    = $data->dt_itemList;
      
      if(($itemList == 0) && ($dt_itemList == 0))
           {
             $bot->SendMsg('[Sell] Plugin Sell: Nothing to do..');
             return;
           }

    $data->Sell = 0;
    $data->itemList = 0;
	  $data->dt_itemList = 0;
    $bot->ld->SavePlSettings("Sellplugin", $data);
   
    $bot->SendMsg('[Sell] *******************************');
    $bot->SendMsg('[Sell] Start Sell Plugin');
    $now = time();

    $bot->SendMsg('[Sell] *******************************');
    
	  if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
    
    $ReloadN = 50;
    while($ReloadN > 0)
         {
         $res=$bot->ld->GetSelect("select * from objects");
         
         if(count($res) > 10)
          {
            $bot->SendMsg("[Sell] *** Main Reload worked ***");
            $ReloadN = 0;
          }
          else { $bot->SendMsg("[Sell] *** Ops... there is an Error  $ReloadN /50*");  return;}
          $ReloadN--;
          }
	  foreach ($res as $val)
          {
            $v=(string)$val[1];
            $obj=unserialize(base64_decode($v));
			      $exist = in_array($obj['id'],$itemList);
            
            if($exist)
              {
                SellObject($bot, $obj);
              }
           }
    if (isset($bot->error_msg)) {  $bot->SendMsg('[Sell] Exit by Error');   }

	  if ($bot->currentWorld != "world_downtown") $bot->LoadWorldObjects("world_downtown");
    
    $ReloadN = 50;
    while($ReloadN > 0)
         {
          $res=$bot->ld->GetSelect("select * from dt_objects");
          
          if(count($res) > 10)
            {
              $bot->SendMsg("[Sell] *** DT Reload worked ***");
              $ReloadN = 0;
            }
              else { $bot->SendMsg("[Sell] *** Ops... there is an Error  $ReloadN /50*");  return;}
              $ReloadN--;
          }
	  foreach ($res as $val)
         {
            $v=(string)$val[1];
            $obj=unserialize(base64_decode($v));
			      $exist = in_array($obj['id'],$dt_itemList);
            if($exist)
            {
              SellObject($bot, $obj);
            }
          }
    if (isset($bot->error_msg)) {  $bot->SendMsg('[Sell] Exit by Error');   }

    $bot->SendMsg('[Sell] Finish Sell Plugin');
    $bot->SendMsg('[Sell] *******************************');
    
	  if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main"); 
    
    $bot->ReloadConfig();
	  $bot->pm->RefreshMePlugin("Sell");

    } 
      
?> 
 



